package com.reverie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reverie.entity.Attribute;
import com.reverie.entity.GoodsAttr;
import com.reverie.mapper.GoodsAttrMapper;
import com.reverie.service.IAttributeService;
import com.reverie.service.IGoodsAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Rui.Zhou
 * @since 2024-02-03
 */
@Service
public class GoodsAttrServiceImpl extends ServiceImpl<GoodsAttrMapper, GoodsAttr> implements IGoodsAttrService {

    @Autowired
    private IAttributeService attributeService;

    @Override
    public List<GoodsAttr> listByGoodIds(Collection<Integer> goodsIds) {
        QueryWrapper<GoodsAttr> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(GoodsAttr::getGoodsId,goodsIds);
        wrapper.lambda().orderByAsc(GoodsAttr::getGoodsAttrId);
        List<GoodsAttr> attrList = list(wrapper);

        Set<Integer>  attrIds = new HashSet<>();
        attrList.forEach(goodsAttr -> attrIds.add(goodsAttr.getAttrId()));
        if(!CollectionUtils.isEmpty(attrIds)){
           List<Attribute> attributes = attributeService.listByAttrIds(attrIds);
            attrList.forEach(goodAttr -> {
                attributes.forEach(attribute -> {
                    if(goodAttr.getAttrId().equals(attribute.getAttrId())){
                        goodAttr.setAttrName(attribute.getAttrName());
                    }
                });
            });
        }

        return attrList;
    }
}
